package com.hn.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.Map;
public class JwtUtils {
    private static String signKey = "heguangle";
    private static Long expire = 1000*60*60L;
    //创建jwt令牌
    public static String getJwt(Map<String,Object> claims){
        String jwt= Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256,signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))//有效期1小时
                .compact();
        return jwt;
    }
    //解析
    public static Claims parseJWT(String jwt){
        //claims记录了用户名和密码
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
